﻿// <copyright file="BillToDetailsConverter.cs" company="Microsoft Corporation">
// Copyright (c) 2008 All Right Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2009-03-03</date>
// <summary>A Value Converter</summary>

namespace NYTimes.ValueConverters
{
    using System;
    using System.Globalization;
    using System.Windows;
    using System.Windows.Data;
    using NYTimes.Data;

    /// <summary>
    /// A Value converter
    /// </summary>
    public class BillToDetailsConverter : IValueConverter
    {
        #region IValueConverter Members

        /// <summary>
        /// Modifies the source data before passing it to the target for display in the UI. 
        /// </summary>
        /// <param name="value">The source data being passed to the target </param>
        /// <param name="targetType">The Type of data expected by the target dependency property.</param>
        /// <param name="parameter">amendments | subjects | related</param>
        /// <param name="culture">The culture of the conversion.</param>
        /// <returns>The value to be passed to the target dependency property. </returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase", Justification = "Not relevant for Silverlight.")]
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return null;
            }

            var bill = value as Bill;

            CongressBill congressBill = null;

            if (parameter == null)
            {
                return null;
            }

            switch (parameter.ToString().ToLower(CultureInfo.InvariantCulture))
            {
                case "amendments":
                    congressBill = new CongressBill()
                    {
                        BillId = bill.UrlNumber,
                        CongressNumber = bill.Congress,
                        Resource = BillResource.Amendments,
                    };
                    return congressBill.Amendments;

                case "subjects":
                    congressBill = new CongressBill()
                    {
                        BillId = bill.UrlNumber,
                        CongressNumber = bill.Congress,
                        Resource = BillResource.Subjects,
                    };
                    return congressBill.Subjects;

                case "related":
                    congressBill = new CongressBill()
                    {
                        BillId = bill.UrlNumber,
                        CongressNumber = bill.Congress,
                        Resource = BillResource.Related,
                    };
                    return congressBill.RelatedBills;
            }

            return null;
        }

        /// <summary>
        /// Modifies the target data before passing it to the source object. This method is called only in TwoWay bindings. 
        /// </summary>
        /// <param name="value">The target data being passed to the source.</param>
        /// <param name="targetType">The Type of data expected by the source object.</param>
        /// <param name="parameter">An optional parameter to be used in the converter logic. </param>
        /// <param name="culture">The culture of the conversion.</param>
        /// <returns>The value to be passed to the source object.</returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return DependencyProperty.UnsetValue;
        }

        #endregion
    }
}
